package org.mogoo.simple.socket2;

import sun.org.mozilla.javascript.internal.ast.NewExpression;

import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * Created with IntelliJ IDEA.
 * Author: Fang
 * Date: 2012-08-21
 * Time: 19:48
 * Email: dongfang@mogoo.org
 */
public class Client1
{
    private Socket socket = null;

    public static void main(String[] args) throws Exception
    {
        new Client1().startClient();
    }

    public void startClient() throws Exception
    {
        socket = new Socket("127.0.0.1", 8888);
        //login();

    }

    public void login() throws Exception
    {

        OutputStream outputStream = socket.getOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
        RequestBody requestBody = new RequestBody();
        requestBody.setRequestType(RequestType.login);
        requestBody.setClientID(1);
        requestBody.setDestinationID(2);
        objectOutputStream.writeObject(requestBody);
        objectOutputStream.close();

        Thread sendThread = new Thread(new SendMessage("dend from clinet 1!!!"));
        sendThread.start();

        Thread receiveThread = new Thread(new ReceiveMessage());
        receiveThread.start();
    }

    class SendMessage implements Runnable
    {
        private String message;

        public SendMessage(String message)
        {
            this.message = message;
        }

        public void run()
        {
            try
            {
                OutputStream outputStream = socket.getOutputStream();
                ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
                RequestBody requestBody = new RequestBody();
                requestBody.setRequestType(RequestType.message);
                requestBody.setClientID(1);
                requestBody.setDestinationID(2);
                requestBody.setMessageBody(message);
                objectOutputStream.writeObject(requestBody);
                objectOutputStream.close();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    }


    class ReceiveMessage implements Runnable
    {
        public void run()
        {
            try
            {
                InputStream inputStream = socket.getInputStream();
                ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
                while(true)
                {
                    RequestBody requestBody = (RequestBody)objectInputStream.readObject();

                    System.out.println("Client 1 received: " + requestBody.getMessageBody() + " from client " + requestBody.getClientID());
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    }


}
